/**
 * 
 */
package com.shine.icis.blo.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.shine.icis.blo.ISubmitConfService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.dao.IDataCheckDAO;
import com.shine.icis.dao.IMetadataCheckLogDAO;
import com.shine.icis.dao.ISubmitConfDAO;
import com.shine.icis.hbean.DataCheckDef;
import com.shine.icis.hbean.MetadataCheckItem;
import com.shine.icis.hbean.SubmitConf;

/**
 * @author zsy
 *
 */
public class SubmitConfService implements ISubmitConfService {
	
	private ISubmitConfDAO submitConfDAO;
	private IDataCheckDAO dataCheckDAO;
	private IMetadataCheckLogDAO metadataCheckDAO;

	public void setSubmitConfDAO(ISubmitConfDAO submitConfDAO) {
		this.submitConfDAO = submitConfDAO;
	}
	

	public void setDataCheckDAO(IDataCheckDAO dataCheckDAO) {
		this.dataCheckDAO = dataCheckDAO;
	}


	public void setMetadataCheckDAO(IMetadataCheckLogDAO metadataCheckDAO) {
		this.metadataCheckDAO = metadataCheckDAO;
	}

	
	@Override
	public List<SubmitConf> getSubmitConfByType(String type, String status) {
		if (type == null) {
			return null;
		}
		return submitConfDAO.findByType(type, status);
	}

//	@Override
//	public List<DictOption> getDictOptions(String groupCode) {
//		return submitConfDAO.findDictOptions(groupCode);
//	}
	
	@Override
	public void modifySubmitConfs(Map<String, String> confs) {
		if (confs == null || confs.isEmpty()) {
			return;
		}
		for (String code : confs.keySet()) {
			SubmitConf cfg = submitConfDAO.findByCode(code, SubmitConf.DATA_SUBMIT);
			if (cfg == null) {
				throw new IllegalArgumentException("配置不存在");
			}
			if (!SubmitConf.STATUS_EDITABLE.equals(cfg.getStatus())) {
				throw new IllegalArgumentException("配置不不可编辑");
			}
			cfg.setValue(confs.get(code));
			submitConfDAO.update(cfg);
		}
		//重载字典
		SysCodeDictLoader.getInstance().initSysConfig();
	}

	@Override
	public int getNextTradeDate(int date) {
		return submitConfDAO.findNextTradeDate(date);
	}

	@Override
	public int getPreviousTradeDate(int date) {
		return submitConfDAO.findPreviousTradeDate(date);
	}
	
	@Override
	public String getPreviousTradeDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String date = sdf.format(new Date());
		int ptd = submitConfDAO.findPreviousTradeDate(Integer.parseInt(date));
		StringBuffer _date = new StringBuffer(Integer.toString(ptd));
		_date.insert(6, "-").insert(4, "-");
		return _date.toString();
	}

	@Override
	public void modifyCheckConfs(Map<Integer, Character> mciMap,
			Map<Integer, Character> dcdMap) {
		if (mciMap != null) {
			List<MetadataCheckItem> mcis = metadataCheckDAO.findMetadataCheckItems(MetadataCheckItem.ENABLE);
			for (MetadataCheckItem mci : mcis) {
				Character value = mciMap.get(mci.getItemID());
				if (value != null) {
					mci.setEnableFlag(value.charValue() == MetadataCheckItem.ON ? MetadataCheckItem.ON : MetadataCheckItem.OF);
					metadataCheckDAO.save(mci);
				}
			}
		}
		
		if (dcdMap != null) {
			List<DataCheckDef> dcds = dataCheckDAO.findDataCheckDefs(DataCheckDef.ENABLE);
			for (DataCheckDef dcd : dcds) {
				Character value = dcdMap.get(dcd.getDefId());
				if (value != null) {
					dcd.setEnableFlag(value.charValue() == DataCheckDef.ON ? DataCheckDef.ON : DataCheckDef.OF);
					dataCheckDAO.save(dcd);
				}
			}
		}
	}
}

